package com.jtny.nytb.powermonitor.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.jtny.component.redis.core.RedisValueService;
import com.jtny.component.util.BeanUtils;
import com.jtny.nytb.datafilling.service.EnergyPriceConfigService;
import com.jtny.nytb.datafilling.vo.InstitutionEquipmentVO;
import com.jtny.nytb.equipment.dto.EquipmentDTO;
import com.jtny.nytb.equipment.dto.PowerMonitorDTO;
import com.jtny.nytb.equipment.service.PowerMonitorService;
import com.jtny.nytb.powermonitor.service.PowerMonitorIterationService;
import com.jtny.nytb.powermonitor.vo.EquipmentVO;
import com.jtny.nytb.powermonitor.vo.MonitorItemVO;
import com.jtny.nytb.powermonitor.vo.PowerMonitorVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 区域配电监测迭代bizImpl类
 *
 * @author 智慧能源中心-车智斌
 * @version 1.0
 * @package com.jtny.nytb.powermonitor.biz.impl;
 * @fileName PowerMonitorIterationBizImpl.java
 * @createTime 2023年12月19日 15:30
 * @Copyright© 2023 jtny
 * 江投能源技术研究院 版权所有
 */
@Service
@AllArgsConstructor
public class PowerMonitorIterationServiceImpl implements PowerMonitorIterationService {

    private EnergyPriceConfigService energyPriceConfig;
    private PowerMonitorService powerMonitorService;
    private RedisValueService redisValueService;
//    private DataParamAlgorithmServiceClient dataParamAlgorithmServiceClient;
//    private DataParamClient dataParamClient;
//    private RestTemplateConfig restTemplateConfig;
//    private DataAcquisitionFeign dataAcquisitionFeign;
//    private DataAcquisitionProperty dataAcquisitionProperty;
//    private EquipmentInfoClient equipmentInfoClient;
//    private AlarmInfoClient alarmInfoClient;
    private static final String IS_EQUIPMENT_RUNNING = "onlineStatus:";
//    private static final String URL = "http://172.18.1.20:5000/get_recent_data";
    //时间格式化对象
    private static final SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm");


    @Override
    public MonitorItemVO queryAll() {

        List<PowerMonitorDTO> powerMonitorDTOS = powerMonitorService.queryAll();

        List<PowerMonitorVO> powerMonitorVOS = BeanUtils.copyToList(powerMonitorDTOS, PowerMonitorVO.class);

        Map<Long, PowerMonitorVO> map = new HashMap<>();

        Set<Integer> treeLevels = new HashSet<>();

        //将配电监测信息放入map中
        for (PowerMonitorVO powerMonitorVO : powerMonitorVOS) {
            map.put(powerMonitorVO.getId(), powerMonitorVO);
            treeLevels.add(powerMonitorVO.getLevel());
        }

        List<PowerMonitorVO> list = new ArrayList<>();

        for (PowerMonitorVO powerMonitorVO : powerMonitorVOS) {
            //判断是否为顶级节点
            if (powerMonitorVO.getParentId() == 0) {
                list.add(powerMonitorVO);
            } else {
                Long parentId = powerMonitorVO.getParentId();
                //在map中获取父节点信息
                PowerMonitorVO parent = map.get(parentId);
                //若map中存在父节点信息
                if (parent != null) {
                    List<PowerMonitorVO> children = parent.getChildren();
                    children.add(powerMonitorVO);
                }
            }
        }

        MonitorItemVO monitorItemVO = new MonitorItemVO();

        Integer treeLevel = treeLevels.stream().max(Integer::compareTo).get();

        monitorItemVO.setTreeData(list);

        monitorItemVO.setTreeLevel(treeLevel);

        return monitorItemVO;
    }

//    @Override
//    public EquipmentsStateVO queryAllEquipmentsState(PowerMonitorVO powerMonitorVO) {
//
//        EquipmentsStateVO equipmentsStateVO = new EquipmentsStateVO();
//        //获取当前时间
//        Date now = new Date();
//        String nowTime = s.format(now);
//        equipmentsStateVO.setTime(nowTime);
//        //初始化正常和非正常设备数量
//        int abnormal = 0;
//        int normal = 0;
//
//        //递归获取子节点所有节点id
//        List<Long> list = getChildPoints(powerMonitorVO.getChildren());
//
//        list.add(powerMonitorVO.getId());
//        //查询出所有设备
//        List<EquipmentDTO> equipmentDTOS = powerMonitorClient.queryEquipmentsByArea(list);
//
//        for (EquipmentDTO equipmentDTO : equipmentDTOS) {
//            //从redis中取值判断设备是否正常运行
//            if (redisValueService.get(IS_EQUIPMENT_RUNNING + equipmentDTO.getDockIdentifier()) != null &&
//                    redisValueService.get(IS_EQUIPMENT_RUNNING + equipmentDTO.getDockIdentifier()).trim() != "") {
//
//                normal = normal + 1;
//            } else {
//
//                abnormal = abnormal + 1;
//            }
//        }
//        equipmentsStateVO.setNormal(String.valueOf(normal));
//        equipmentsStateVO.setAbnormal(String.valueOf(abnormal));
//
//        return equipmentsStateVO;
//    }

    @Override
    public List<EquipmentVO> queryEquipments(PowerMonitorVO powerMonitorVO) {

        PowerMonitorDTO powerMonitorDTO = BeanUtils.copyToObject(powerMonitorVO, PowerMonitorDTO.class);

        List<EquipmentDTO> equipmentDTOS = powerMonitorService.queryEquipments(powerMonitorDTO);

        if (equipmentDTOS != null) {
            for (EquipmentDTO equipmentDTO : equipmentDTOS) {
                //从redis中取值判断设备是否正常运行
                if (redisValueService.get(IS_EQUIPMENT_RUNNING + equipmentDTO.getDockIdentifier()) != null &&
                        redisValueService.get(IS_EQUIPMENT_RUNNING + equipmentDTO.getDockIdentifier()).trim() != "") {
                    equipmentDTO.setIsRunning("1");
                } else {
                    equipmentDTO.setIsRunning("0");
                }
            }
        }
        return BeanUtils.copyToList(equipmentDTOS, EquipmentVO.class);
    }

    @Override
    public List<EquipmentVO> queryEqByAreaForInsu(PowerMonitorVO powerMonitorVO) {
        PowerMonitorDTO powerMonitorDTO = BeanUtils.copyToObject(powerMonitorVO, PowerMonitorDTO.class);

        List<EquipmentDTO> equipmentDTOS = powerMonitorService.queryEquipments(powerMonitorDTO);

        if (equipmentDTOS != null) {
            for (EquipmentDTO equipmentDTO : equipmentDTOS) {
                //从redis中取值判断设备是否正常运行
                if (redisValueService.get(IS_EQUIPMENT_RUNNING + equipmentDTO.getDockIdentifier()) != null &&
                        redisValueService.get(IS_EQUIPMENT_RUNNING + equipmentDTO.getDockIdentifier()).trim() != "") {
                    equipmentDTO.setIsRunning("1");
                } else {
                    equipmentDTO.setIsRunning("0");
                }
            }
        }

        InstitutionEquipmentVO institutionEquipmentVO = new InstitutionEquipmentVO();
        institutionEquipmentVO.setYear(String.valueOf(LocalDate.now().getYear()));
        institutionEquipmentVO.setMonth(String.valueOf(LocalDate.now().getMonthValue()));
        List<InstitutionEquipmentVO> institutionEquipment = energyPriceConfig.getInstitutionEquipment(institutionEquipmentVO);
        if(CollectionUtil.isNotEmpty(institutionEquipment) && CollectionUtil.isNotEmpty(equipmentDTOS)){
            List<Long> eqIds = institutionEquipment.stream().map(InstitutionEquipmentVO::getEquipmentId).collect(Collectors.toList());
            List<EquipmentDTO> results = equipmentDTOS.stream().filter(e -> !eqIds.contains(e.getId())).collect(Collectors.toList());
            return BeanUtils.copyToList(results, EquipmentVO.class);
        }

        return BeanUtils.copyToList(equipmentDTOS, EquipmentVO.class);
    }

//    @Override
//    public Map<String, Object> queryDataParam(EquipmentVO equipmentVO) {
//        //设置返回数据参量集合
//        List<DataVO> list = new ArrayList<>();
//
//        Map<String, DataParamVO> map = new TreeMap<>();
//        //根据设备查询参量详情id及是否展示
//        List<DataParamAlgorithmEquipmentETLDTO> etlList = dataParamAlgorithmServiceClient.listByEquipmentCode(equipmentVO.getCode());
//        //返回的参量集合不为空时
//        if (CollUtil.isNotEmpty(etlList)) {
//            for (DataParamAlgorithmEquipmentETLDTO paramDTO : etlList) {
//                //参量数据中为展示配电监测时
//                if (paramDTO.getPowerCheckDisplay() != null &&
//                        paramDTO.getPowerCheckDisplay() &&
//                        paramDTO.getDataParamId() != null) {
//                    //通过参数id查询具体参数数据
//                    DataParamVO dataParamVO = BeanUtils.copyToObject(paramDTO, DataParamVO.class);
//                    map.put(paramDTO.getDataParamId().toString(), dataParamVO);
//                }
//            }
//        }
//        TreeMap<String, String> treeMap = queryUrlValue(map);
//
//        Map<String, Object> result = new TreeMap<>();
//        //获取网关标识
//        String gatewayIdentifier = treeMap.get("param");
//
//        String[] strings = gatewayIdentifier.split("_");
//
//        result.put("param1", strings[0]);
//        result.put("param2", strings[1]);
//        //移除手动添加的参数
//        treeMap.remove("param");
//        //时间集合
//        List<String> times = new ArrayList<>();
//
//        if (map != null && map.size() > 0 && treeMap != null && !treeMap.isEmpty()) {
//            //遍历返回的map
//            for (Map.Entry<String, String> entry : treeMap.entrySet()) {
//                //切割时间和值
//                String[] s = entry.getValue().split(";");
//                //将获取的时间放入时间集合中
//                times.add(s[1]);
//            }
//            //将时间以降序进行去重并排列
//            List<String> collect = times.stream().distinct().sorted(Comparator.comparing(String::valueOf).reversed()).collect(Collectors.toList());
//
//            if (!collect.isEmpty()) {
//                //获取最新更新时间
//                String time = collect.get(0);
//                result.put("lastUpdateTime", time);
//            }
//            for (Map.Entry<String, String> entry : treeMap.entrySet()) {
//
//                if (equipmentVO.getRemoteControl() && map.get(entry.getKey()).getReportIdentifier().equals("Sw_open")) {
//
//                    String[] split = entry.getValue().split(";");
//
//                    if (split[0].equals("0.00")) {
//                        result.put("isOpen", 0);
//                    } else if (split[0].equals("1.00")) {
//                        result.put("isOpen", 1);
//                    }
//                }
//                DataVO DataVO = new DataVO();
//                String a = treeMap.get(entry.getKey());
//                String[] split = a.split(";");
//                DataVO.setKey(map.get(entry.getKey()).getUnifiedIdentifierName());
//                DataVO.setUnit(map.get(entry.getKey()).getMeasurementUnit());
//                DataVO.setValue(split[0]);
//                list.add(DataVO);
//            }
//        }
//        result.put("data", list);
//        return result;
//
//    }
//
//
//    @Override
//    public Map<String, Object> queryDataParamByArea(PowerMonitorVO powerMonitorVO) {
//        Map<String, Object> result = new HashMap<>();
//        EquipmentModelPageQueryParamDTO queryParamDTO = new EquipmentModelPageQueryParamDTO();
//        //递归获取子节点所有节点id
//        List<Long> list = getChildPoints(powerMonitorVO.getChildren());
//        // 添加当前节点id
//        list.add(powerMonitorVO.getId());
//        queryParamDTO.setAreaIdList(list);
//        queryParamDTO.setStatus(GenericStatusEnum.NORMAL.getValue());
//        List<EquipmentInfoDTO> equipmentList = equipmentInfoClient.listByQueryParam(queryParamDTO);
//        if (CollUtil.isEmpty(equipmentList)) {
//            return result;
//        }
//        //将所有设备的code存入集合
//        QueryETLDTO queryETLDTO = new QueryETLDTO();
//        queryETLDTO.setUnifiedIdentifierList(CollUtil.newArrayList(EPI, E, WATER_CONSUMPTION, SF));
//        queryETLDTO.setEquipmentCodeList(equipmentList.stream()
//                .map(EquipmentInfoDTO::getCode)
//                .collect(Collectors.toList()));
//        List<DataParamAlgorithmEquipmentETLDTO> etlList = dataParamAlgorithmServiceClient.queryList(queryETLDTO);
//        if (CollUtil.isEmpty(etlList)) {
//            return result;
//        }
//
//        // 按统一标识分组 每组数据后续用于合计
//        Map<String, List<DataParamAlgorithmEquipmentETLDTO>> tempMap = etlList.stream()
//                .collect(Collectors.groupingBy(DataParamAlgorithmEquipmentETLDTO::getUnifiedIdentifier, Collectors.toList()));
//        ResponseBaseDTO<ResponseCurrentDiffDataDTO> currentDiffData = dataAcquisitionFeign
//                .getCurrentDiffData(dataAcquisitionProperty.getUrl(),
//                        buildRequestCurrentDiffData(tempMap));
//
//        DataAcquisitionResponseUtils.handler(currentDiffData);
//
//        return buildResponseCurrentDiffData(currentDiffData);
//    }

    // 正向有功电能
    public static final String EPI = "EPI";
    // 综合能耗
    public static final String E = "E";
    // 用水量
    public static final String WATER_CONSUMPTION = "water_consumption";
    // 碳排放量
    public static final String SF = "SF";
    public static final String DAY = "d";
    public static final String MONTH = "mo";
    public static final String YEAR = "y";
    public static final List<String> NOW_TIME = CollUtil.newArrayList(DAY, MONTH, YEAR);

//    private Map<String, Object> buildResponseCurrentDiffData(ResponseBaseDTO<ResponseCurrentDiffDataDTO> currentDiffDataDTO) {
//        List<DataVO> result = new ArrayList<>();
//        List<ResponseCurrentDiffDataDTO> data = currentDiffDataDTO.getData();
//        for (ResponseCurrentDiffDataDTO currentDiffData : data) {
//            Set<String> missing = currentDiffData.getInputParameter().keySet();
//
//            LinkedHashMap<String, ResponseCurrentDiffParameterDTO> outParameter = currentDiffData.getOutParameter();
//            outParameter.remove(SF);
//            outParameter.remove(E);
//            outParameter.remove(EPI);
//            outParameter.remove(WATER_CONSUMPTION);
//            double daySum = outParameter.values().stream().mapToDouble(item -> item.getD() != null ? item.getD().getDiffValue() : 0).sum();
//            double monthSum = outParameter.values().stream().mapToDouble(item -> item.getMo() != null ? item.getMo().getDiffValue() : 0).sum();
//            double yearSum = outParameter.values().stream().mapToDouble(item -> item.getY() != null ? item.getY().getDiffValue() : 0).sum();
//            // 统计正向有功电能
//            if (missing.contains(SF)) {
//                result.add(new DataVO().buildSF(daySum, "日"));
//                result.add(new DataVO().buildSF(monthSum, "月"));
//                result.add(new DataVO().buildSF(yearSum, "年"));
//            }
//            // 综合能耗
//            if (missing.contains(E)) {
//                result.add(new DataVO().buildE(daySum, "日"));
//                result.add(new DataVO().buildE(monthSum, "月"));
//                result.add(new DataVO().buildE(yearSum, "年"));
//            }
//            // 正向有功电能
//            if (missing.contains(EPI)) {
//                result.add(new DataVO().buildEPI(daySum, "日"));
//                result.add(new DataVO().buildEPI(monthSum, "月"));
//                result.add(new DataVO().buildEPI(yearSum, "年"));
//            }
//            // 用水量
//            if (missing.contains(WATER_CONSUMPTION)) {
//                result.add(new DataVO().buildWaterConsumption(daySum, "日"));
//                result.add(new DataVO().buildWaterConsumption(monthSum, "月"));
//                result.add(new DataVO().buildWaterConsumption(yearSum, "年"));
//            }
//        }
//        Map<String, Object> map = new HashMap<>();
//        map.put("data", result);
//        map.put("lastUpdateTime", DateUtil.now());
//        return map;
//    }
//
//
//    /**
//     * 构建请求当前差异数据
//     *
//     * @param tempMap 条件
//     * @return {@link RequestBaseDTO}<{@link RequestCurrentDiffDataDTO}>
//     */
//    private RequestBaseDTO<RequestCurrentDiffDataDTO> buildRequestCurrentDiffData(Map<String, List<DataParamAlgorithmEquipmentETLDTO>> tempMap) {
//        String orgCode = LocalHolder.getCurrentUser().getTenantId();
//        RequestBaseDTO<RequestCurrentDiffDataDTO> result = new RequestBaseDTO<>();
//        List<RequestCurrentDiffDataDTO> data = new ArrayList<>();
//        for (String unifiedIdentifier : tempMap.keySet()) {
//            RequestCurrentDiffDataDTO requestCurrentDiffDataDTO = new RequestCurrentDiffDataDTO();
//            requestCurrentDiffDataDTO.setNowTime(NOW_TIME);
//            requestCurrentDiffDataDTO.setOrgCode(orgCode);
//            LinkedHashMap<String, String> inputParameter = new LinkedHashMap<>();
//            tempMap.get(unifiedIdentifier).forEach(dataParamAlgorithmEquipmentETLDTO -> {
//                inputParameter.put(dataParamAlgorithmEquipmentETLDTO.getCode(), dataParamAlgorithmEquipmentETLDTO.getCode());
//            });
//            // 新增加标志位 后期一定会在missing中
//            inputParameter.put(unifiedIdentifier, unifiedIdentifier);
//            // 设置输入参数
//            requestCurrentDiffDataDTO.setInputParameter(inputParameter);
//            data.add(requestCurrentDiffDataDTO);
//        }
//        result.setData(data);
//        return result;
//    }
//
//    @Override
//    public Map<String, Object> queryDataParamByEquipment(EquipmentVO equipmentVO) {
//        Map<String, Object> result = new HashMap<>();
//        EquipmentInfoDTO byId = equipmentInfoClient.getById(equipmentVO.getId());
//        if (ObjectUtil.isEmpty(byId)) {
//            return result;
//        }
//        //将所有设备的code存入集合
//        QueryETLDTO queryETLDTO = new QueryETLDTO();
//        queryETLDTO.setUnifiedIdentifierList(CollUtil.newArrayList(EPI, E, WATER_CONSUMPTION, SF));
//        queryETLDTO.setEquipmentCode(byId.getCode());
//        List<DataParamAlgorithmEquipmentETLDTO> etlList = dataParamAlgorithmServiceClient.queryList(queryETLDTO);
//        if (CollUtil.isEmpty(etlList)) {
//            return result;
//        }
//        // 按统一标识分组 每组数据后续用于合计
//        Map<String, List<DataParamAlgorithmEquipmentETLDTO>> tempMap = etlList.stream()
//                .collect(Collectors.groupingBy(DataParamAlgorithmEquipmentETLDTO::getUnifiedIdentifier, Collectors.toList()));
//        ResponseBaseDTO<ResponseCurrentDiffDataDTO> currentDiffData = dataAcquisitionFeign
//                .getCurrentDiffData(dataAcquisitionProperty.getUrl(),
//                        buildRequestCurrentDiffData(tempMap));
//        DataAcquisitionResponseUtils.handler(currentDiffData);
//        return buildResponseCurrentDiffData(currentDiffData);
//    }
//
//    @Override
//    public List<EquipmentMonitorVO> queryEquipmentMonitor() {
//        //查询所有网关设备
//        List<EquipmentDTO> gwEquipments = powerMonitorClient.queryGWEquipments();
//
//        List<EquipmentMonitorVO> gwEquipmentMonitorVOS = BeanUtils.copyToList(gwEquipments, EquipmentMonitorVO.class);
//        //查询所有正常设备
//        List<EquipmentDTO> normalEquipmentDTOS = powerMonitorClient.queryNormalEquipments();
//
//        List<EquipmentMonitorVO> normalEquipmentMonitorVOS = BeanUtils.copyToList(normalEquipmentDTOS, EquipmentMonitorVO.class);
//
//        Map<String, EquipmentMonitorVO> map = new HashMap<>();
//
//        normalEquipmentMonitorVOS.stream().forEach(a -> map.put(a.getDockIdentifier(), a));
//
//        for (EquipmentMonitorVO equipmentMonitorVO : gwEquipmentMonitorVOS) {
//            //初始化正常和异常设备个数
//            int normal = 0;
//            int abnormal = 0;
//            //从redis中取值判断设备是否正常运行
//            if (redisValueService.get(IS_EQUIPMENT_RUNNING + equipmentMonitorVO.getDockIdentifier()) != null &&
//                    redisValueService.get(IS_EQUIPMENT_RUNNING + equipmentMonitorVO.getDockIdentifier()).trim() != "") {
//                equipmentMonitorVO.setIsRunning("1");
//                equipmentMonitorVO.setTime("-");
//            } else {
//                equipmentMonitorVO.setIsRunning("0");
//                DataParamDTO dataParamDTO = dataParamClient.queryDataParamByCode(equipmentMonitorVO.getCode());
//                AlarmInfoDTO alarmInfoByParamId = alarmInfoClient.getAlarmInfoByParamId(dataParamDTO.getDataParamId().toString());
//                equipmentMonitorVO.setTime(alarmInfoByParamId.getAlarmTime());
//            }
//
//            List<EquipmentMonitorVO> list = new ArrayList<>();
//            //通过网关设备标识获取普通设备的设备标识
//            List<EquipmentGatewayRelationDTO> equipmentGatewayRelationDTOS
//                    = powerMonitorClient.queryNormalIdByGWId(equipmentMonitorVO.getDockIdentifier());
//
//            for (EquipmentGatewayRelationDTO equipmentGatewayRelationDTO : equipmentGatewayRelationDTOS) {
//
//                EquipmentMonitorVO equipmentMonitor = map.get(equipmentGatewayRelationDTO.getMcbId());
//                //从redis中取值判断设备是否正常运行
//                if (redisValueService.get(IS_EQUIPMENT_RUNNING + equipmentMonitor.getDockIdentifier()) != null &&
//                        redisValueService.get(IS_EQUIPMENT_RUNNING + equipmentMonitor.getDockIdentifier()).trim() != "") {
//                    equipmentMonitor.setIsRunning("1");
//                    equipmentMonitor.setTime("-");
//                    normal = normal + 1;
//                } else {
//                    equipmentMonitor.setIsRunning("0");
//                    DataParamDTO dataParam = dataParamClient.queryDataParamByCode(equipmentMonitor.getCode());
//                    AlarmInfoDTO alarmInfo = alarmInfoClient.getAlarmInfoByParamId(dataParam.getDataParamId().toString());
//                    equipmentMonitor.setTime(alarmInfo.getAlarmTime());
//                    abnormal = abnormal + 1;
//                }
//                list.add(equipmentMonitor);
//            }
//            equipmentMonitorVO.setNormal(String.valueOf(normal));
//            equipmentMonitorVO.setAbnormal(String.valueOf(abnormal));
//            equipmentMonitorVO.setChildren(list);
//        }
//        return gwEquipmentMonitorVOS;
//    }
//
//    @Override
//    public List<EquipmentVO> queryDirectEquipment(Long id) {
//
//        PowerMonitorDTO powerMonitorDTO = new PowerMonitorDTO();
//        powerMonitorDTO.setId(id);
//        List<EquipmentDTO> equipmentDTOS = powerMonitorClient.queryEquipments(powerMonitorDTO);
//        //获取与网关汇集设备
//        List<EquipmentDTO> equipmentDTOSByGW = powerMonitorClient.queryEquipmentsByGW();
//
//        equipmentDTOS.removeAll(equipmentDTOSByGW);
//
//        if (!equipmentDTOS.isEmpty()){
//            for (EquipmentDTO equipmentDTO : equipmentDTOS) {
//                //从redis中取值判断设备是否正常运行
//                if (redisValueService.get(IS_EQUIPMENT_RUNNING + equipmentDTO.getDockIdentifier()) != null &&
//                        redisValueService.get(IS_EQUIPMENT_RUNNING + equipmentDTO.getDockIdentifier()).trim() != ""){
//                    equipmentDTO.setIsRunning("1");
//                }else {
//                    equipmentDTO.setIsRunning("0");
//                }
//            }
//        }
//        return BeanUtils.copyToList(equipmentDTOS,EquipmentVO.class);
//    }
//
//    @Override
//    public EquipmentsStateVO queryAllGWEquipmentsState(PowerMonitorVO powerMonitorVO) {
//
//        EquipmentsStateVO equipmentsStateVO = new EquipmentsStateVO();
//        //获取当前时间
//        Date now = new Date();
//        String nowTime = s.format(now);
//        equipmentsStateVO.setTime(nowTime);
//        //初始化正常和非正常设备数量
//        int abnormal = 0;
//        int normal = 0;
//
//        //递归获取子节点所有节点id
//        List<Long> list = getChildPoints(powerMonitorVO.getChildren());
//
//        list.add(powerMonitorVO.getId());
//        //查询出所有设备
//        List<EquipmentDTO> equipmentDTOS = powerMonitorClient.queryGWEquipmentsByArea(list);
//
//        for (EquipmentDTO equipmentDTO : equipmentDTOS) {
//            //从redis中取值判断设备是否正常运行
//            if (redisValueService.get(IS_EQUIPMENT_RUNNING + equipmentDTO.getDockIdentifier()) != null &&
//                    redisValueService.get(IS_EQUIPMENT_RUNNING + equipmentDTO.getDockIdentifier()).trim() != "") {
//
//                normal = normal + 1;
//            } else {
//
//                abnormal = abnormal + 1;
//            }
//        }
//        equipmentsStateVO.setNormal(String.valueOf(normal));
//        equipmentsStateVO.setAbnormal(String.valueOf(abnormal));
//
//        return equipmentsStateVO;
//    }
//
//    @Override
//    public List<EquipmentMonitorVO> queryEquipmentMonitorByGW(List<EquipmentVO> equipmentVOS) {
//
//        List<EquipmentMonitorVO> gwEquipmentMonitorVOS = BeanUtils.copyToList(equipmentVOS, EquipmentMonitorVO.class);
//        //查询所有正常设备
//        List<EquipmentDTO> normalEquipmentDTOS = powerMonitorClient.queryNormalEquipments();
//
//        List<EquipmentMonitorVO> normalEquipmentMonitorVOS = BeanUtils.copyToList(normalEquipmentDTOS, EquipmentMonitorVO.class);
//
//        Map<String, EquipmentMonitorVO> map = new HashMap<>();
//
//        normalEquipmentMonitorVOS.stream().forEach(a -> map.put(a.getDockIdentifier(), a));
//
//        for (EquipmentMonitorVO equipmentMonitorVO : gwEquipmentMonitorVOS) {
//            //初始化正常和异常设备个数
//            int normal = 0;
//            int abnormal = 0;
//            //从redis中取值判断设备是否正常运行
//            if (redisValueService.get(IS_EQUIPMENT_RUNNING + equipmentMonitorVO.getDockIdentifier()) != null &&
//                    redisValueService.get(IS_EQUIPMENT_RUNNING + equipmentMonitorVO.getDockIdentifier()).trim() != "") {
//                equipmentMonitorVO.setIsRunning("1");
//                equipmentMonitorVO.setTime("-");
//            } else {
//                equipmentMonitorVO.setIsRunning("0");
//                DataParamDTO dataParamDTO = dataParamClient.queryDataParamByCode(equipmentMonitorVO.getCode());
//                AlarmInfoDTO alarmInfoByParamId = alarmInfoClient.getAlarmInfoByParamId(dataParamDTO.getDataParamId().toString());
//                equipmentMonitorVO.setTime(alarmInfoByParamId.getAlarmTime());
//            }
//
//            List<EquipmentMonitorVO> list = new ArrayList<>();
//            //通过网关设备标识获取普通设备的设备标识
//            List<EquipmentGatewayRelationDTO> equipmentGatewayRelationDTOS
//                    = powerMonitorClient.queryNormalIdByGWId(equipmentMonitorVO.getDockIdentifier());
//
//            for (EquipmentGatewayRelationDTO equipmentGatewayRelationDTO : equipmentGatewayRelationDTOS) {
//
//                EquipmentMonitorVO equipmentMonitor = map.get(equipmentGatewayRelationDTO.getMcbId());
//                //从redis中取值判断设备是否正常运行
//                if (redisValueService.get(IS_EQUIPMENT_RUNNING + equipmentMonitor.getDockIdentifier()) != null &&
//                        redisValueService.get(IS_EQUIPMENT_RUNNING + equipmentMonitor.getDockIdentifier()).trim() != "") {
//                    equipmentMonitor.setIsRunning("1");
//                    equipmentMonitor.setTime("-");
//                    normal = normal + 1;
//                } else {
//                    equipmentMonitor.setIsRunning("0");
//                    DataParamDTO dataParam = dataParamClient.queryDataParamByCode(equipmentMonitor.getCode());
//                    AlarmInfoDTO alarmInfo = alarmInfoClient.getAlarmInfoByParamId(dataParam.getDataParamId().toString());
//                    equipmentMonitor.setTime(alarmInfo.getAlarmTime());
//                    abnormal = abnormal + 1;
//                }
//                list.add(equipmentMonitor);
//            }
//            equipmentMonitorVO.setNormal(String.valueOf(normal));
//            equipmentMonitorVO.setAbnormal(String.valueOf(abnormal));
//            equipmentMonitorVO.setChildren(list);
//        }
//        return gwEquipmentMonitorVOS;
//    }
//
//    @Override
//    public List<EquipmentVO> queryGWEquipment(Long id) {
//
//        List<EquipmentDTO> equipmentDTOS = powerMonitorClient.queryGWEquipmentsByAreaId(id);
//
//        if (!equipmentDTOS.isEmpty()){
//            for (EquipmentDTO equipmentDTO : equipmentDTOS) {
//                //从redis中取值判断设备是否正常运行
//                if (redisValueService.get(IS_EQUIPMENT_RUNNING + equipmentDTO.getDockIdentifier()) != null &&
//                        redisValueService.get(IS_EQUIPMENT_RUNNING + equipmentDTO.getDockIdentifier()).trim() != ""){
//                    equipmentDTO.setIsRunning("1");
//                }else {
//                    equipmentDTO.setIsRunning("0");
//                }
//            }
//        }
//        return BeanUtils.copyToList(equipmentDTOS,EquipmentVO.class);
//    }
//
//    //通过参量编码获取参数值
//    private TreeMap<String, String> queryUrlValue(Map<String, DataParamVO> map) {
//        //设置查询参数
//        TreeMap<String, String> inputParamMap = new TreeMap<>();
//
//        for (Map.Entry<String, DataParamVO> entry : map.entrySet()) {
//
//            if (entry.getValue().getGatewayEquipmentIdentifier() != null
//                    && !entry.getValue().getGatewayEquipmentIdentifier().trim().equals("")) {
//
//                inputParamMap.put(entry.getKey(), entry.getValue().getGatewayEquipmentIdentifier());
//            }
//        }
//        //初始化返回的结果集对象
//        TreeMap<String, String> value = new TreeMap<>();
//
//        value.put("param", inputParamMap.firstEntry().getValue());
//
//        Map<String, Object> paramMap = new HashMap<>();
//
//        CurrentUserDTO currentUser = LocalHolder.getCurrentUser();
//        String tenantId = currentUser.getTenantId();
//
//        paramMap.put("org_code", tenantId);
//        paramMap.put("input_parameter", inputParamMap);
//        paramMap.put("recent_type", "ods");
//
//        List<Map<String, Object>> data = new ArrayList();
//        data.add(paramMap);
//
//        Map<String, Object> hashMap = new HashMap<>();
//        hashMap.put("data", data);
//
//        HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(hashMap, null);
//
//        String url = dataAcquisitionProperty.getUrl().toString() + "/get_recent_data";
//        Map<String, Object> result = restTemplateConfig.getRestTemplate().postForObject(url, httpEntity, Map.class);
//
//        if ("200".equals(result.get("code").toString())) {
//
//            List list = (List) result.get("data");
//
//            Map<String, Object> map1 = (Map<String, Object>) list.get(0);
//
//            String isSuccess = map1.get("success").toString().trim();
//
//            if (isSuccess.equals("成功")) {
//
//                Map<String, Object> map2 = (Map<String, Object>) map1.get("out_parameter");
//
//                for (Map.Entry<String, DataParamVO> entry : map.entrySet()) {
//
//                    Map<String, Object> aMap = (Map<String, Object>) map2.get(entry.getKey());
//
//                    if (aMap != null && !aMap.isEmpty()) {
//
//                        List aValue = (List) aMap.get("_value");
//
//                        List aTime = (List) aMap.get("_time");
//
//                        String b = (String) aTime.get(0);
//
//                        String c = b.substring(0, 10) + " " + b.substring(11, 19);
//
//                        String s = aValue.get(0).toString();
//
//                        BigDecimal v = new BigDecimal(new Double(s));
//
//                        String a = v.setScale(2, BigDecimal.ROUND_HALF_UP).toString();
//                        value.put(entry.getKey(), a + ";" + c);
//                    }
//                }
//
//                return value;
//            }
//        }
//        return null;
//    }
//
//    //获取区域下所有子节点id
//    private List<Long> getChildPoints(List<PowerMonitorVO> powerMonitorVOS) {
//        List<Long> list = new ArrayList<>();
//        for (PowerMonitorVO powerMonitorVO : powerMonitorVOS) {
//            list.add(powerMonitorVO.getId());
//            getChildPoints(powerMonitorVO.getChildren());
//        }
//        return list;
//    }
//
//
//    private void getUrlData(List<DataParamCodeDTO> dataParamByCodes) {
//        //获取当前企业id
//        CurrentUserDTO currentUser = LocalHolder.getCurrentUser();
//        String tenantId = currentUser.getTenantId();
//
//        for (DataParamCodeDTO dataParamCodeDTO : dataParamByCodes) {
//
//            RequestCurrentDiffDataDTO requestCurrentDiffDataDTO = new RequestCurrentDiffDataDTO();
//            requestCurrentDiffDataDTO.setOrgCode(tenantId);
//            requestCurrentDiffDataDTO.setNowTime(NOW_TIME);
//            LinkedHashMap<String, String> map = new LinkedHashMap<>();
//
//            List<String> codes = dataParamCodeDTO.getCodes();
//
//            for (String str : codes) {
//                String[] split = str.split(":");
//                map.put(split[0], split[1]);
//            }
//            List<RequestCurrentDiffDataDTO> requestCurrentDiffDataDTOS = new ArrayList<>();
//            requestCurrentDiffDataDTO.setInputParameter(map);
//            requestCurrentDiffDataDTOS.add(requestCurrentDiffDataDTO);
//            RequestBaseDTO<RequestCurrentDiffDataDTO> requestDTO = new RequestBaseDTO<>();
//            requestDTO.setData(requestCurrentDiffDataDTOS);
////            String nowTime = JSONArray.toJSONString(requestDTO);
////            System.out.println(nowTime);
//            ResponseBaseDTO<ResponseCurrentDiffDataDTO> currentDiffData = dataAcquisitionFeign.getCurrentDiffData(dataAcquisitionProperty.getUrl(), requestDTO);
////            System.out.println(currentDiffData);
//        }
//    }
}
